nanopyx.core.transform.image_magnify

Combination of functions for zooming an image, using several interpolation methods.

  1"""
  2Combination of functions for zooming an image, using several interpolation methods.
  3"""
  4
  5import numpy as np
  6from cv2 import INTER_LANCZOS4
  7from cv2 import resize as cv2_resize
  8from scipy.ndimage import zoom
  9from skimage.transform import rescale
 10
 11from . import interpolation_fft_zoom
 12
 13from ..utils.timeit import timeit2
 14from . import interpolation_bicubic
 15from . import interpolation_bilinear
 16from . import interpolation_nearest_neighbor
 17from . import interpolation_catmull_rom
 18from . import interpolation_lanczos
 19
 20
 21@timeit2
 22def fourier_zoom(image: np.ndarray, magnification: float = 2) -> np.ndarray:
 23    """
 24    Zoom an image by zero-padding its Discrete Fourier transform.
 25    :param image: 2D grid of pixel values.
 26    :param magnification: Factor by which to multiply the dimensions of the image.
 27    :return: zoomed image.
 28    """
 29
 30    return interpolation_fft_zoom.magnify(image, magnification)
 31
 32
 33@timeit2
 34def catmull_rom_zoom(image: np.ndarray, magnification: int = 2) -> np.ndarray:
 35    """
 36    Zoom an image by Catmull-Rom interpolation
 37    :param image: 2D grid of pixel values.
 38    :param magnification: Factor by which to multiply the dimensions of the image.
 39    :return: zoomed image.
 40    REF: based on https://github.com/HenriquesLab/NanoJ-SRRF/blob/master/SRRF/src/nanoj/srrf/java/SRRF.java
 41    """
 42
 43    interpolator = interpolation_catmull_rom.Interpolator(image)
 44    return interpolator.magnify(magnification)
 45
 46def catmull_rom_zoom_xy(image: np.ndarray, magnification_y: int = 2, magnification_x: int = 2) -> np.ndarray:
 47    interpolator = interpolation_catmull_rom.Interpolator(image)
 48    return interpolator.magnify_xy(magnification_y, magnification_x)
 49
 50@timeit2
 51def lanczos_zoom(
 52    image: np.ndarray, magnification: int = 2) -> np.ndarray:
 53    """
 54    Zoom an image by Lanczos interpolation
 55    :param image: 2D grid of pixel values.
 56    :param magnification: Factor by which to multiply the dimensions of the image.
 57    :param taps: The number of taps (interpolation points) to use in the Lanczos kernel.
 58    :return: zoomed image.
 59    """
 60
 61    interpolator = interpolation_lanczos.Interpolator(image)
 62    return interpolator.magnify(magnification)
 63
 64
 65@timeit2
 66def bicubic_zoom(image: np.ndarray, magnification: int = 2) -> np.ndarray:
 67    """
 68    Zoom an image by bicubic interpolation
 69    :param image: 2D grid of pixel values.
 70    :param magnification: Factor by which to multiply the dimensions of the image.
 71    :return: zoomed image.
 72    """
 73
 74    interpolator = interpolation_bicubic.Interpolator(image)
 75    return interpolator.magnify(magnification)
 76
 77
 78@timeit2
 79def bilinear_zoom(image: np.ndarray, magnification: int = 2) -> np.ndarray:
 80    """
 81    Zoom an image by bilinear interpolation
 82    :param image: 2D grid of pixel values.
 83    :param magnification: Factor by which to multiply the dimensions of the image.
 84    :return: zoomed image.
 85    """
 86
 87    interpolator = interpolation_bilinear.Interpolator(image)
 88    return interpolator.magnify(magnification)
 89
 90
 91@timeit2
 92def nearest_neighbor_zoom(
 93    image: np.ndarray, magnification: int = 2
 94) -> np.ndarray:
 95    """
 96    Zoom an image by nearest neighbor interpolation
 97    :param image: 2D grid of pixel values.
 98    :param magnification: Factor by which to multiply the dimensions of the image.
 99    :return: zoomed image.
100    """
101
102    interpolator = interpolation_nearest_neighbor.Interpolator(image)
103    return interpolator.magnify(magnification)
104
105
106@timeit2
107def scipy_zoom(image: np.ndarray, magnification: int = 2) -> np.ndarray:
108    """
109    Zoom an image by SciPy interpolation
110    :param image: 2D grid of pixel values.
111    :param magnification: Factor by which to multiply the dimensions of the image.
112    :return: zoomed image.
113    """
114
115    return zoom(image, magnification)
116
117
118@timeit2
119def skimage_zoom(image: np.ndarray, magnification: int = 2) -> np.ndarray:
120    """
121    Zoom an image by scikit-image interpolation
122    :param image: 2D grid of pixel values.
123    :param magnification: Factor by which to multiply the dimensions of the image.
124    :return: zoomed image.
125    """
126
127    return rescale(image, magnification, anti_aliasing=False)
128
129
130@timeit2
131def cv2_zoom(image: np.ndarray, magnification: int = 2) -> np.ndarray:
132    """
133    Zoom an image by OpenCV interpolation
134    :param image: 2D grid of pixel values.
135    :param magnification: Factor by which to multiply the dimensions of the image.
136    :return: zoomed image.
137    """
138
139    return cv2_resize(
140        image,
141        None,
142        fx=magnification,
143        fy=magnification,
144        interpolation=INTER_LANCZOS4,
145    )
def fourier_zoom(*args, **kwargs):
18    def wrapper(*args, **kwargs):
19        start = time.time()
20        result = func(*args, **kwargs)
21        end = time.time()
22        delta = end - start
23        if delta < 10**-4:
24            msg = f"{func.__name__} took {delta/1e-6:.6f} nseconds"
25        elif delta < 10**-1:
26            msg = f"{func.__name__} took {delta/1e-3:.6f} mseconds"
27        else:
28            msg = f"{func.__name__} took {delta:.6f} seconds"
29        print(msg)
30        return result

Zoom an image by zero-padding its Discrete Fourier transform.

Parameters
  • image: 2D grid of pixel values.
  • magnification: Factor by which to multiply the dimensions of the image.
Returns

zoomed image.

def catmull_rom_zoom(*args, **kwargs):
18    def wrapper(*args, **kwargs):
19        start = time.time()
20        result = func(*args, **kwargs)
21        end = time.time()
22        delta = end - start
23        if delta < 10**-4:
24            msg = f"{func.__name__} took {delta/1e-6:.6f} nseconds"
25        elif delta < 10**-1:
26            msg = f"{func.__name__} took {delta/1e-3:.6f} mseconds"
27        else:
28            msg = f"{func.__name__} took {delta:.6f} seconds"
29        print(msg)
30        return result

Zoom an image by Catmull-Rom interpolation

Parameters
  • image: 2D grid of pixel values.
  • magnification: Factor by which to multiply the dimensions of the image.
Returns

zoomed image. REF: based on https://github.com/HenriquesLab/NanoJ-SRRF/blob/master/SRRF/src/nanoj/srrf/java/SRRF.java

def catmull_rom_zoom_xy( image: numpy.ndarray, magnification_y: int = 2, magnification_x: int = 2) -> numpy.ndarray:
47def catmull_rom_zoom_xy(image: np.ndarray, magnification_y: int = 2, magnification_x: int = 2) -> np.ndarray:
48    interpolator = interpolation_catmull_rom.Interpolator(image)
49    return interpolator.magnify_xy(magnification_y, magnification_x)
def lanczos_zoom(*args, **kwargs):
18    def wrapper(*args, **kwargs):
19        start = time.time()
20        result = func(*args, **kwargs)
21        end = time.time()
22        delta = end - start
23        if delta < 10**-4:
24            msg = f"{func.__name__} took {delta/1e-6:.6f} nseconds"
25        elif delta < 10**-1:
26            msg = f"{func.__name__} took {delta/1e-3:.6f} mseconds"
27        else:
28            msg = f"{func.__name__} took {delta:.6f} seconds"
29        print(msg)
30        return result

Zoom an image by Lanczos interpolation

Parameters
  • image: 2D grid of pixel values.
  • magnification: Factor by which to multiply the dimensions of the image.
  • taps: The number of taps (interpolation points) to use in the Lanczos kernel.
Returns

zoomed image.

def bicubic_zoom(*args, **kwargs):
18    def wrapper(*args, **kwargs):
19        start = time.time()
20        result = func(*args, **kwargs)
21        end = time.time()
22        delta = end - start
23        if delta < 10**-4:
24            msg = f"{func.__name__} took {delta/1e-6:.6f} nseconds"
25        elif delta < 10**-1:
26            msg = f"{func.__name__} took {delta/1e-3:.6f} mseconds"
27        else:
28            msg = f"{func.__name__} took {delta:.6f} seconds"
29        print(msg)
30        return result

Zoom an image by bicubic interpolation

Parameters
  • image: 2D grid of pixel values.
  • magnification: Factor by which to multiply the dimensions of the image.
Returns

zoomed image.

def bilinear_zoom(*args, **kwargs):
18    def wrapper(*args, **kwargs):
19        start = time.time()
20        result = func(*args, **kwargs)
21        end = time.time()
22        delta = end - start
23        if delta < 10**-4:
24            msg = f"{func.__name__} took {delta/1e-6:.6f} nseconds"
25        elif delta < 10**-1:
26            msg = f"{func.__name__} took {delta/1e-3:.6f} mseconds"
27        else:
28            msg = f"{func.__name__} took {delta:.6f} seconds"
29        print(msg)
30        return result

Zoom an image by bilinear interpolation

Parameters
  • image: 2D grid of pixel values.
  • magnification: Factor by which to multiply the dimensions of the image.
Returns

zoomed image.

def nearest_neighbor_zoom(*args, **kwargs):
18    def wrapper(*args, **kwargs):
19        start = time.time()
20        result = func(*args, **kwargs)
21        end = time.time()
22        delta = end - start
23        if delta < 10**-4:
24            msg = f"{func.__name__} took {delta/1e-6:.6f} nseconds"
25        elif delta < 10**-1:
26            msg = f"{func.__name__} took {delta/1e-3:.6f} mseconds"
27        else:
28            msg = f"{func.__name__} took {delta:.6f} seconds"
29        print(msg)
30        return result

Zoom an image by nearest neighbor interpolation

Parameters
  • image: 2D grid of pixel values.
  • magnification: Factor by which to multiply the dimensions of the image.
Returns

zoomed image.

def scipy_zoom(*args, **kwargs):
18    def wrapper(*args, **kwargs):
19        start = time.time()
20        result = func(*args, **kwargs)
21        end = time.time()
22        delta = end - start
23        if delta < 10**-4:
24            msg = f"{func.__name__} took {delta/1e-6:.6f} nseconds"
25        elif delta < 10**-1:
26            msg = f"{func.__name__} took {delta/1e-3:.6f} mseconds"
27        else:
28            msg = f"{func.__name__} took {delta:.6f} seconds"
29        print(msg)
30        return result

Zoom an image by SciPy interpolation

Parameters
  • image: 2D grid of pixel values.
  • magnification: Factor by which to multiply the dimensions of the image.
Returns

zoomed image.

def skimage_zoom(*args, **kwargs):
18    def wrapper(*args, **kwargs):
19        start = time.time()
20        result = func(*args, **kwargs)
21        end = time.time()
22        delta = end - start
23        if delta < 10**-4:
24            msg = f"{func.__name__} took {delta/1e-6:.6f} nseconds"
25        elif delta < 10**-1:
26            msg = f"{func.__name__} took {delta/1e-3:.6f} mseconds"
27        else:
28            msg = f"{func.__name__} took {delta:.6f} seconds"
29        print(msg)
30        return result

Zoom an image by scikit-image interpolation

Parameters
  • image: 2D grid of pixel values.
  • magnification: Factor by which to multiply the dimensions of the image.
Returns

zoomed image.

def cv2_zoom(*args, **kwargs):
18    def wrapper(*args, **kwargs):
19        start = time.time()
20        result = func(*args, **kwargs)
21        end = time.time()
22        delta = end - start
23        if delta < 10**-4:
24            msg = f"{func.__name__} took {delta/1e-6:.6f} nseconds"
25        elif delta < 10**-1:
26            msg = f"{func.__name__} took {delta/1e-3:.6f} mseconds"
27        else:
28            msg = f"{func.__name__} took {delta:.6f} seconds"
29        print(msg)
30        return result

Zoom an image by OpenCV interpolation

Parameters
  • image: 2D grid of pixel values.
  • magnification: Factor by which to multiply the dimensions of the image.
Returns

zoomed image.